#include <gtest/gtest.h>
#include "common.h"
#include <unordered_set>

using namespace ::std;

class Solution {
public:
    ListNode *detectCycle(ListNode *head);
};

ListNode* Solution::detectCycle(ListNode *head) {
    unordered_set<ListNode*> s;
    int cnt = 0;
    while(head != nullptr) {
        if(s.find(head) == s.end()) s.insert(head);
        else return head;
        head = head->next;
    }
    return nullptr;
}

class DetectCycleCheck : public ::testing::Test {
public:
	class Solution solution;
};

TEST_F(DetectCycleCheck, testDetectCycle_1) {
    int pos = 1;
    vector<int> value = {3,2,0,-4};
    ListNode* head = makeCycleLinkedList(value, pos);
    EXPECT_TRUE(head->next == solution.detectCycle(head));
}

TEST_F(DetectCycleCheck, testDetectCycle_2) {
    int pos = 0;
    vector<int> value = {1,2};
    ListNode* head = makeCycleLinkedList(value, pos);
    EXPECT_TRUE(head == solution.detectCycle(head));
}

TEST_F(DetectCycleCheck, testDetectCycle_3) {
    int pos = -1;
    vector<int> value = {1};
    ListNode* head = makeCycleLinkedList(value, pos);
    EXPECT_TRUE(nullptr == solution.detectCycle(head));
}


int main() {
	::testing::InitGoogleTest();
	return RUN_ALL_TESTS();
}
